<template>
  <el-card class="box-card">
    <div class="tools">
      <!--  搜索框-->
      <div class="tools-left">
        <el-input
            v-model="urlParams.search"
            placeholder="请输入关键字"
            @keyup.enter="onSearch"
            clearable
            @clear="onSearch"
            class="search"
        />
        <el-button type="primary" @click="onSearch">
          <el-icon>
            <search/>
          </el-icon>&nbsp;搜索
        </el-button>
      </div>

      <div class="tools-right">
        <el-button type="primary" @click="createDialogVisible=true">
          <el-icon>
            <Plus/>
          </el-icon>&nbsp;创建
        </el-button>
        <!--展示列弹出框-->
        <el-popover placement="left" :width="100" v-model:visible="columnVisible">
          <template #reference>
            <el-button type="primary" @click="columnVisible=true">
              <el-icon>
                <setting/>
              </el-icon>&nbsp;展示列
            </el-button>
          </template>
          <el-checkbox v-model="showColumn.name" disabled>机房名称</el-checkbox>
          <el-checkbox v-model="showColumn.city">城市</el-checkbox>
          <el-checkbox v-model="showColumn.provider">运营商</el-checkbox>
          <el-checkbox v-model="showColumn.note">备注</el-checkbox>
          <el-checkbox v-model="showColumn.create_time">创建时间</el-checkbox>
          <!--    新增选择内容持久化配置-->
          <div style="text-align: right; margin: 0">
            <el-button size="small" type="text" @click="columnVisible = false">取消</el-button>
            <el-button size="small" type="primary" @click="saveColumn">确认</el-button>
          </div>
        </el-popover>
      </div>
    </div>
    <el-table
        :data="tableData"
        border="1px"
        style="width: 100%"
    >
      <el-table-column prop="name" label="机房名称"/>
      <el-table-column prop="city" label="所在城市" v-if="showColumn.city"/>
      <el-table-column prop="provider" label="提供商" v-if="showColumn.provider"/>
      <el-table-column prop="note" label="备注" v-if="showColumn.note"/>
      <el-table-column prop="create_time" label="创建时间" v-if="showColumn.create_time"/>
      <el-table-column flexd label="操作栏" width="150px">
        <template #default="scope">
          <el-button type="primary" size="small" @click="EditIdc(scope.$index,scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="DeleteIdc(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页-->
    <div style="margin-top: 20px">
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[10, 15, 20, 25, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </el-card>
  <!--  idc编辑-->
  <IdcEdit v-model:visible="editDialogVisible" :row="currentRow"></IdcEdit>

  <!--  idc创建-->
  <IdcCreate v-model:visible="createDialogVisible"></IdcCreate>

</template>

<script>
import IdcEdit from "@/views/idc/IdcEdit";
import IdcCreate from "@/views/idc/IdcCreate";

export default {
  components: {
    IdcEdit,
    IdcCreate
  },
  name: "Idc",
  data() {
    return {
      tableData: '',
      currentPage: 1,   //默认第一页
      pageSize: 10,  //默认每页10条
      total: 0,      //总条数
      urlParams: {
        page_num: 1,
        page_size: 10,
        search: ''
      },
      currentRow: '',
      editDialogVisible: false,
      createDialogVisible: false,
      columnVisible: false, // 可展示列显示与隐藏
      showColumn: {
        name: true,
        city: true,
        provider: true,
        note: true,
        create_time: true
      }
    }
  },
  methods: {
    getData() {
      this.$http.get('cmdb/idc/', {params: this.urlParams})
          .then(res => {
            this.tableData = res.data.data;
            this.total = res.data.count;

          })
    },
    //监听每页数量的事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.urlParams.page_size = pageSize;
      this.getData()
    },
    //监听页码变动的事件
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage; // 重新设置分页显示
      this.urlParams.page_num = currentPage;
      this.getData()
    },
    EditIdc(index, row) {
      this.editDialogVisible = true;
      this.currentRow = row; //将当前行内容传递到子组件
    },
    DeleteIdc(index, row) {
      this.$confirm("你确定要删除选中的吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {  // 点击确定
            this.$http.delete('/cmdb/idc/' + row.id + '/')
                .then(res => {
                  if (res.data.code === 200) {
                    this.$message.success(res.data.msg);
                    this.tableData.splice(index, 1); // 根据表格索引临时删除数据
                  }
                });
          })
    },
    onSearch() {
      this.getData()
    },
    saveColumn() {
      // 将可显示的字段存储到浏览器本地存储
      localStorage.setItem(this.$route.path + '-columnSet', JSON.stringify(this.showColumn));
      this.columnVisible = false;
    },
  },
  mounted() {
    this.getData()
    const columnSet = localStorage.getItem(this.$route.path + '-columnSet');
    if (columnSet) {
      this.showColumn = JSON.parse(columnSet)
    }
  },
}
</script>

<style scoped>
.tools {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.tools-left {
  display: flex;
}

.tools-right {
  display: flex;
}

.search {
  width: 150px;
  margin-right: 10px;
}
</style>